home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / cpp114.zoo / src / alloc.cg next >
Encoding:
Text File  |  1995-01-20  |  1.5 KB  |  92 lines

  1. #include <stdlib.h>
  2. #include "global.h"
  3.  
  4. #ifdef DEBUG
  5. #define FREED    0x8000
  6. #endif
  7.  
  8. @0
  9.  
  10. typedef struct BlockOf_@1 {
  11.   struct BlockOf_@1 *next;
  12.   @1 block[@2];
  13. } BlockOf_@1;
  14.  
  15. static struct {
  16.   BlockOf_@1 *blocklist;
  17.   @1 *freelist;
  18. #ifdef DEBUG
  19.   int num_blocks;
  20. #endif
  21. } Arena_@1 = {0, 0
  22. #ifdef DEBUG
  23.           , 0
  24. #endif
  25.              };
  26.  
  27. static void getblock_@1()
  28. {
  29.   register BlockOf_@1 *B =
  30.     (BlockOf_@1 *)mallok(sizeof (BlockOf_@1));
  31.   register int i;
  32.  
  33.   for (i = 0; i < @2 - 1; i++) {
  34.     B->block[i].next = &(B->block[i + 1]);
  35. #ifdef DEBUG
  36.     B->block[i].flags = FREED;
  37. #endif
  38.   }
  39.   B->block[@2 - 1].next = Arena_@1.freelist;
  40. #ifdef DEBUG
  41.   B->block[@2 - 1].flags = FREED;
  42. #endif
  43.   Arena_@1.freelist = B->block;
  44.   B->next = Arena_@1.blocklist;
  45.   Arena_@1.blocklist = B;
  46. #ifdef DEBUG
  47.   Arena_@1.num_blocks++;
  48. #endif
  49. }
  50.  
  51. @1 *alloc_@1()
  52. {
  53.   register @1 *T = Arena_@1.freelist;
  54.  
  55.   if (!T) {getblock_@1(); T = Arena_@1.freelist;}
  56.   Arena_@1.freelist = T->next; T->next = 0;
  57. #ifdef DEBUG
  58.   T->flags &= ~FREED;
  59. #endif
  60.   return T;
  61. }
  62.  
  63. void dealloc_@1(T) register @1 *T;
  64. {
  65.   T->next = Arena_@1.freelist;
  66.   Arena_@1.freelist = T;
  67. #ifdef DEBUG
  68.   T->flags |= FREED;
  69. #endif
  70. }
  71.  
  72. void cleanup_@1()
  73. {
  74.   register BlockOf_@1 *B1, *B2;
  75.  
  76.   for (B1 = Arena_@1.blocklist; B1; B1 = B2) {
  77. #ifdef DEBUG
  78.     register int i, n = 0;
  79.  
  80.     for (i = 0; i < @2; i++) {
  81.       if (!(B1->block[i].flags & FREED))
  82.     n++;
  83.     }
  84.     fprintf(stderr, "%d @1 blocks allocated\n", Arena_@1.num_blocks);
  85.     if (n > 0)
  86.       bugchk("%d @1 struct(s) leaked", n);
  87. #endif
  88.     B2 = B1->next;
  89.     free(B1);
  90.   }
  91. }
  92.